# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        ret = []
        queue = [root] if root else []
        while queue:
            ret.append([r.val for r in queue])
            newQueue = []
            for r in queue:
                if r.left: newQueue.append(r.left)
                if r.right: newQueue.append(r.right)
            queue = newQueue
        return ret